package com.example.demo2.service.impl;

import com.example.demo2.entity.UploadedData;
import com.example.demo2.mapper.UploadedDataMapper;
import com.example.demo2.service.UploadedDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author
 * @version 1.0
 * @description TODO
 * @date 2024/7/6 21:34
 */
@Service
public class UploadedDataServiceImpl implements UploadedDataService {

    @Autowired
    private UploadedDataMapper uploadedDataMapper;


    @Override
    public List<Map<String, String>> getAllUploadedDataNames() {
        return uploadedDataMapper.findAllUploadNames();
    }


    @Override
    public Page<UploadedData> getUploadedDataByName(String uploadName, int page, int size) {
        int offset = page * size;
        List<UploadedData> data = uploadedDataMapper.findByUploadName(uploadName, offset, size);
        long total = uploadedDataMapper.countByUploadName(uploadName);
        return new PageImpl<>(data, PageRequest.of(page, size), total);
    }

    @Override
    public void insertUploadedData(UploadedData data) {
        uploadedDataMapper.insertUploadedData(data);
    }

    @Override
    public int countByUploadName(String uploadName) {
        return uploadedDataMapper.countByUploadName(uploadName);
    }

    @Override
    public List<UploadedData> getUploadedDataByNameBatch(String uploadName, int offset, int batchSize) {
        return uploadedDataMapper.findByNameBatch(uploadName, offset, batchSize);
    }
    @Override
    public List<String> getAllUploadNames() {
        List<Map<String, String>> uploadNamesMap = uploadedDataMapper.findAllUploadNames();
        return uploadNamesMap.stream()
                .map(map -> map.get("uploadName"))
                .collect(Collectors.toList());
    }
}